package co.yixiang.hw.rest;
import co.yixiang.hw.service.YxUserService;
import co.yixiang.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;

public class BaseController {

    @Autowired
    private YxUserService userService;

    public UserDetails getUser(){
        UserDetails userDetails = SecurityUtils.getUserDetails();
        return userDetails;
    }

    public Long getUserId(){
        if(getUserName() == null){
            return -1L;
        }else{
            return userService.finByUserName(getUserName()).getUid();
        }
    }

    public String  getUserName(){
        try {
            return SecurityUtils.getUsername();
        } catch (Exception e){
            return null;
        }
    }
}
